﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace UniversityManagementSystem.UI
{
    public partial class ViewAllUnscheduledCourses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Department department = new Department();
                    Semester semester = new Semester();
                    List<Department> allDepartments = new List<Department>();
                    List<Semester> allSemesters = new List<Semester>();
                    allSemesters = semester.GetAllSemesters();
                    allDepartments = department.GetAllDepartments();
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataTextField = "DepartmentName";
                    departmentDropDownList.DataValueField = "DepartmentCode";
                    departmentDropDownList.DataBind();
                    semesterDropDownList.DataSource = allSemesters;
                    semesterDropDownList.DataTextField = "SemesterName";
                    semesterDropDownList.DataValueField = "SemesterId";
                    semesterDropDownList.DataBind();
                }
            }
            catch (Exception)
            {
                messageLabel.Text = "Error occured while loading the page.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void showButton_Click(object sender, EventArgs e)
        {
            try
            {
                List<Course> unschedeuledCourses = new List<Course>();
                Course course = new Course();
                unschedeuledCourses = course.ShowUnschedeuledCourses(departmentDropDownList.SelectedValue, semesterDropDownList.SelectedValue);
                unscheduledCourseDataGridView.DataSource = unschedeuledCourses;
                unscheduledCourseDataGridView.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Error occured";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void pdfButton_Click(object sender, EventArgs e)
        {
            try
            {
                List<Course> unschedeuledCourses = new List<Course>();
                Course course = new Course();
                unschedeuledCourses = course.ShowUnschedeuledCourses(departmentDropDownList.SelectedValue, semesterDropDownList.SelectedValue);
                unscheduledCourseDataGridView.DataSource = unschedeuledCourses;
                unscheduledCourseDataGridView.DataBind();

                Document document = null;

                document = new Document(PageSize.A4, 0, 0, 15, 5);

                PdfPTable mainTable = course.CreatePdfUnscheduledCourses(unscheduledCourseDataGridView, departmentDropDownList.SelectedValue);
                PdfWriter.GetInstance(document, Response.OutputStream);
                document.Open();
                document.Add(mainTable);
                document.Close();

                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename= unscheduledCourses.pdf");
                Response.End();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to create PDF";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}